package com.longway.core.ext.excel.data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class TableDataset implements Iterator,Cloneable
{
    private TableMeta tableMeta;
    private List<List> datatset = new Vector<List>();
    private Integer visitIndex;
    
    public List<List> getDatatset() {
		return datatset;
	}

	public void setDatatset(List<List> datatset) {
		this.datatset = datatset;
	}

	public List<TableDataset> dice(TableDataset tableDataset, int chunkSize) {
        int size = tableDataset.count();
        int chunk_num = size / chunkSize + (size % chunkSize == 0 ? 0 : 1);
        List<TableDataset> result = new ArrayList<TableDataset>();
        for (int i = 0; i < chunk_num; i++) {
        	List<List> res = tableDataset.datatset.subList(i * chunkSize, i == chunk_num - 1 ? size : (i + 1) * chunkSize);
        	TableDataset tds = new TableDataset();
        	tds.setTableMeta(tableMeta);
        	tds.setDatatset(res);
        	result.add(tds);
        }
        return result;
    }
    
    public TableMeta getTableMeta()
    {
        return tableMeta;
    }

    public void setTableMeta(TableMeta tableMeta)
    {
        this.tableMeta = tableMeta;
    }

    public int getColumnCount()
    {
        return tableMeta.getColumns().size();
    }

    public String getColumnName(int i)
    {
        if (i < tableMeta.getColumns().size())
        {
            return tableMeta.getColumns().get(i).getColumn();
        }
        return null;
    }

    public boolean hasColumn(String name)
    {
        return tableMeta.hasColumnName(name);
    }

    public ColumnMeta getColumn(int i)
    {
        if (i < tableMeta.getColumns().size())
        {
            return tableMeta.getColumns().get(i);
        }
        return null;
    }

    public boolean add(List rowData)
    {
        if (rowData.size() > tableMeta.getColumns().size())
        {
            return false;
        }

        datatset.add(rowData);
        return true;
    }

    public RowsetData get(int i)
    {
        if (i < datatset.size())
        {
            return new RowsetData(tableMeta, datatset.get(i));
        }

        return null;
    }


    @Override
    public boolean hasNext()
    {
        if (visitIndex == null)
        {
            visitIndex = 0;
        }
        if (visitIndex < datatset.size())
        {
            return true;
        }

        return false;
    }

    @Override
    public RowsetData next()
    {
        if (visitIndex < datatset.size())
        {
            return new RowsetData(tableMeta, datatset.get(visitIndex++));

        }

        return null;
    }

    @Override
    public void remove()
    {
        datatset.clear();
        visitIndex = null;
    }

    public int count()
    {
        return datatset.size();
    }

    public void reset()
    {
        visitIndex = null;
    }
    @Override
    public Object clone(){
    	TableDataset tabledataset = null;
    	try {
			tabledataset = (TableDataset)super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return tabledataset;
    }

	public Integer getVisitIndex() {
		return visitIndex;
	}

}
